package com.example.lab11_p01_menuactionbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		View menuSearch = menu.findItem(R.id.menu0).getActionView();
		final EditText txtSearch = (EditText) menuSearch
				.findViewById(R.id.txtSearch);
		txtSearch
				.setOnEditorActionListener(new TextView.OnEditorActionListener() {

					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						// TODO Auto-generated method stub
						Toast.makeText(getBaseContext(),
								txtSearch.getText().toString(),
								Toast.LENGTH_SHORT).show();
						return false;
					}
				});
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Toast.makeText(this, "Click Setting", Toast.LENGTH_SHORT).show();
			return true;
		} else if (id == R.id.menu1) {
			Toast.makeText(this, "menu 1", Toast.LENGTH_SHORT).show();
			return true;
		} else if (id == R.id.menu2) {
			Toast.makeText(this, "menu 2", Toast.LENGTH_SHORT).show();
			return true;
		} else if (id == R.id.menu3) {
			Toast.makeText(this, "menu 3", Toast.LENGTH_SHORT).show();
			return true;
		} else if (id == R.id.menu4) {
			Toast.makeText(this, "menu 4", Toast.LENGTH_SHORT).show();
			return true;
		} else if (id == R.id.menu5) {
			Toast.makeText(this, "menu 5", Toast.LENGTH_SHORT).show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void showPopmenu(View v) {
		PopupMenu popupMenu = new PopupMenu(getBaseContext(), v);
		MenuInflater inflater = popupMenu.getMenuInflater();
		inflater.inflate(R.menu.popupmenu, popupMenu.getMenu());
		popupMenu.show();
	}
}
